package com.mindmap.ui;
import com.mindmap.model.MindMap;
import com.mindmap.model.Node;
import com.mindmap.ui.MainFrame;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class DrawingPanel extends JPanel {
    private MainFrame mainFrame;
    private MindMap mindMap;

    private static final Color NODE_COLOR = new Color(220, 240, 255);
    private static final Color SELECTED_NODE_COLOR = new Color(180, 220, 255);
    private static final Color LINE_COLOR = new Color(100, 100, 100);
    private static final Font NODE_FONT = new Font("微软雅黑", Font.PLAIN, 14);

    public DrawingPanel(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
        setBackground(Color.WHITE);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                handleMouseClick(e);
            }
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        if (mindMap != null && mindMap.getRoot() != null) {
            drawNode(g2d, mindMap.getRoot());
        }
    }

    private void drawNode(Graphics2D g2d, Node node) {
        // 绘制连接线
        if (node != mindMap.getRoot()) {
            g2d.setColor(LINE_COLOR);
            Point start = node.getParent().getConnectionPoint(node);
            Point end = node.getConnectionPoint(node.getParent());
            g2d.drawLine(start.x, start.y, end.x, end.y);
        }

        // 绘制节点
        Rectangle bounds = node.getBounds();
        if (node.isSelected()) {
            g2d.setColor(SELECTED_NODE_COLOR);
        } else {
            g2d.setColor(NODE_COLOR);
        }
        g2d.fillRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, 10, 10);
        g2d.setColor(Color.BLACK);
        g2d.drawRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, 10, 10);

        // 绘制文本
        g2d.setFont(NODE_FONT);
        FontMetrics fm = g2d.getFontMetrics();
        int textWidth = fm.stringWidth(node.getText());
        int textX = bounds.x + (bounds.width - textWidth) / 2;
        int textY = bounds.y + (bounds.height - fm.getHeight()) / 2 + fm.getAscent();
        g2d.drawString(node.getText(), textX, textY);

        // 绘制子节点
        for (Node child : node.getChildren()) {
            drawNode(g2d, child);
        }
    }

    private void handleMouseClick(MouseEvent e) {
        if (mindMap == null) return;

        Point clickPoint = e.getPoint();
        Node clickedNode = mindMap.findNodeAt(clickPoint);

        if (clickedNode != null) {
            mindMap.setSelectedNode(clickedNode);
            repaint();
        } else {
            // 点击空白处取消选择
            if (mindMap.getSelectedNode() != null) {
                mindMap.setSelectedNode(null);
                repaint();
            }
        }
    }

    public void setMindMap(MindMap mindMap) {
        this.mindMap = mindMap;
    }

    public void exportToImage(File file, String format) {
        BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();
        paint(g2d);
        g2d.dispose();

        try {
            ImageIO.write(image, format, file);
            JOptionPane.showMessageDialog(this, "导出成功!");
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, "导出失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}
